<template>
	<view style="height: 100%;margin-top: 100rpx;">
		<!-- #ifdef H5 -->
		<!-- <view class="iconfont icon-xiangzuo" @click="goback()"></view> -->
		<!-- #endif -->
		<hx-navbar :back="true" :fixed="false" :color="knavTitleColor" barPlaceholder="hidden"
			transparent="auto"></hx-navbar>
		<web-view :src="walletUrl" @message="onMessage"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				walletUrl: "",
				aiEventId: '',
				isAI: false
			}
		},
		onLoad(val) {
			this.isAI = val.ai || false
			if (val.iftype == 'sd') {
				let url = uni.getStorageSync('sdUrl');
				 
				this.walletUrl = url
			} else {
				this.walletUrl = val.url
				var currentUrl = this.walletUrl;
			 
				// 提取查询字符串部分（?后面的部分）  
				const queryString = currentUrl.split('?')[1];
				// 使用split和循环来找到aiEventId的值  
				const params = queryString.split('&').reduce((acc, param) => {
					const [key, value] = param.split('=');
					acc[key] = value;
					return acc;
				}, {});

			}
		 

			// #ifdef APP-PLUS
			var height = 0;
			uni.getSystemInfo({
				success: (sysinfo) => {
					height = sysinfo.windowHeight;
				},
				complete: () => {}
			});
			var currentWebview = this.$scope.$getAppWebview();
			setTimeout(function() {
				this.barHeight = plus.navigator.getStatusbarHeight();
				var wv = currentWebview.children()[0];
				wv.setStyle({
					top: this.barHeight,
					height: height - this.barHeight,
					scalable: true,
				})
			}, 500);

			// #endif



		},
		methods: {
			// 接收到消息以后返回
			onMessage(event) {
				// let pages = getCurrentPages()
				// let page = pages[pages.length - 1];
				// let currentPages = page.$getAppWebview()
				// let children = currentPages.children()
				// // uni.navigateBack()
				// if (children.length === 0) {
				//    uni.setStorageSync('messageType','ai')
				// 	uni.navigateBack({ delta: 4 })

				// } else {
				// 	children[0].close()
				// 	console.log("--",children);
				// 	children[1].close()
				// 	setTimeout(() => {
				// 		uni.navigateBack()
				// 	}, 80)
				// }
				// 	console.log("--",children.length);
				// console.log('web-view的---ai----：', event)
				// return true;

				// uni.setStorageSync('messageType','ai')
				// 获取当前页面栈
				// const pages = getCurrentPages();
				// console.log('页数'+JSON.stringify(pages))
				// setTimeout(()=>{
				//   uni.navigateBack({ delta: 1 });
				// },1000)
			 
				if (this.isAI) {
					uni.setStorageSync('ifAIsd', 'ifAIsd');
				uni.setStorageSync('messageType', 'ai')
					let pages = getCurrentPages()
					let page = pages[pages.length - 1];
					let currentPages = page.$getAppWebview()
					let children = currentPages.children()
					// uni.navigateBack()
					if (children.length === 0) {
						uni.navigateBack({
							delta:4
						})
						
					} else {
						children[0].close()
						 
						children[1].close()
						setTimeout(() => {
							uni.navigateBack()
						}, 80)
					}
				 
					return true;
				} else {
					uni.setStorageSync('ifAIsd', '');
					uni.setStorageSync('messageType', '');
					uni.switchTab({
						url: `/pages/index/index`
					})
				 
				}
				// console.log('this.aiEventId-',this.aiEventId)

			},
		}
	}
</script>

<style>

</style>